package com.lorentzos.swipecards.example;

import com.lorentzos.flingswipe.LogUtil;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import com.lorentzos.swipecards.example.slice.TwoDemoAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.ArrayList;

public class TwoDemoAbility extends Ability implements Component.ClickedListener, SwipeFlingAdapterView.onFlingListener {
    private SwipeFlingAdapterView flingContainer;

    private BaseAdapter mProvider;

    private ArrayList<String> al;

    private int mCount;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(TwoDemoAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_two_demo);

        flingContainer = (SwipeFlingAdapterView) findComponentById(ResourceTable.Id_frame);
        findComponentById(ResourceTable.Id_left).setClickedListener(this);
        findComponentById(ResourceTable.Id_right).setClickedListener(this);
        flingContainer.setClickedListener(this);

        al = new ArrayList<>();
        al.add("php");
        al.add("c");
        al.add("python");
        al.add("java");
        al.add("html");
        al.add("c++");
        al.add("css");
        al.add("javascript");

        mProvider = new BaseAdapter(this);
        mProvider.setData(al);
        flingContainer.setItemProvider(mProvider);
        flingContainer.setFlingListener(this);
    }
    @Override
    public void removeFirstObjectInAdapter() {
        // this is the simplest way to delete an object from the Adapter (/AdapterView)
        LogUtil.debug("LIST", "removed object!");
        al.remove(0);
        mProvider.notifyDataSetItemRemoved(0);
    }

    @Override
    public void onLeftCardExit(Object dataObject) {
        // Do something on the left!
        // You also have access to the original object.
        // If you want to use it just cast it (String) dataObject
        makeToast(TwoDemoAbility.this, "Left!");
    }

    @Override
    public void onRightCardExit(Object dataObject) {
        makeToast(this, "Right!");
    }

    @Override
    public void onAdapterAboutToEmpty(int itemsInAdapter) {
        // Ask for more data here
        al.add("XML ".concat(String.valueOf(mCount)));
        mProvider.notifyDataSetItemInserted(al.size() - 1);
        LogUtil.debug("LIST", "notified");
        mCount++;
    }

    @Override
    public void onScroll(float scrollProgressPercent) {
        Component component = flingContainer.getSelectedComponent();
        component
                .findComponentById(ResourceTable.Id_item_swipe_right_indicator)
                .setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
        component
                .findComponentById(ResourceTable.Id_item_swipe_left_indicator)
                .setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_left) {
            flingContainer.getTopCardListener().selectLeft();
        } else if (component.getId() == ResourceTable.Id_right) {
            flingContainer.getTopCardListener().selectRight();
        } else if (component.getId() == ResourceTable.Id_frame) {
            makeToast(this, "Clicked!");
        }
    }

    private void makeToast(Context context, String text) {
        ToastDialog toast = new ToastDialog(context);
        toast.setText(text);
        // 显示Toast会导致快速滑动删除卡顿
        // toast.show();
    }
}
